"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/7 15:43
@Description : 
编写程序实现用户的注册和登录功能，要求用户名不能重复，密码为六位并且加密处理，用户可选择是登录还是注册
"""
import hashlib
while True:
    print('*' * 20, '欢迎使用xxx管理系统', '*' * 20)
    print('1.注册')
    print('2.登录')
    print('*' * 60)
    user_choice = input('请选择(Q退出):')
    if user_choice.upper() == 'Q':
        break
    match user_choice:
        case '1':
            print('----------------注册------------------')
            with open('file/user_data.txt', 'rt', encoding='UTF-8') as f:
                data_lines = f.readlines()
            while True:
                username = input('请输入用户名:')
                for data_line in data_lines:
                    if data_line.split('@')[0] == username:
                        print('用户名已存在')
                        break
                else:
                    break
            while True:
                password = input('请输入密码:')
                if len(password) == 6:
                    break
                else:
                    print('密码长度必须为6位')
            # 密码加密
            password_hash = hashlib.md5(password.encode()).hexdigest()
            user_info_data = username + '@' + password_hash
            with open('file/user_data.txt', 'a', encoding='UTF-8') as f:
                f.write(user_info_data + '\n')
            print('用户信息注册成功')
        case '2':
            print('----------------登录------------------')
            username = input('请输入用户名:')
            password = input('请输入密码:')
            # 密码加密
            password_hash = hashlib.md5(password.encode()).hexdigest()
            user_info_data = username + '@' + password_hash
            with open('file/user_data.txt', 'rt', encoding='UTF-8') as f:
                data_lines = f.readlines()
            for data_line in data_lines:
                if user_info_data == data_line.strip():
                    print('登录成功')
                    break
            else:
                print('用户名或密码错误')
        case _:
            break

print('----------------系统安全退出----------------------')
